<template>
  <BaseDialog
    ref="baseDialog"
    :title="isEdit ? '编辑组织' : '新增组织'"
    @confrim="confrim"
    @cancel="close"
  >
    <BaseForm
      labelWidth="150px"
      :fromIems="fromIems"
      :size="'medium'"
      ref="BaseForm"
      @submit="submit"
      @edit="edit"
    ></BaseForm>
  </BaseDialog>
</template>

<script>
import { Message } from "element-ui";
import { searchFormParams, uploadImg } from "@/utils";
import { add, edit } from "@/api/org";
import { getId } from "@/api/common";
import _ from "lodash";
export default {
  name: "add",
  data() {
    return {
      fromIems: [
        {
          valueType: "input",
          label: "组织名称",
          dataIndex: "name",
          rules: [{ required: true, message: "请输入", trigger: "blur" }],
          defaultValue: "",

        },
        {
          valueType: "input",
          label: "手机号码",
          dataIndex: "phone",
          defaultValue: "",

          rules: [
            { required: true, message: "请输入", trigger: "blur" },
            {
              pattern: /^1[3-9]\d{9}$/,
              message: "请输入正确的手机号码",
              trigger: "blur",
            },
          ],
        },
        {
          valueType: "input",
          label: "姓名",
          dataIndex: "xm",
          rules: [{ required: true, message: "请输入", trigger: "blur" }],
          defaultValue: "",

        },
        {
          valueType: "img",
          label: "证件照",
          dataIndex: "idUrl",
          uplaod: (e) => this.handleUploadIdCard(e),
          rules: [{ required: true, message: "请上传", trigger: "blur" }],
          defaultValue: "",
        },

        // {
        //   valueType: "img",
        //   label: "营业执照图",
        //   dataIndex: "yyzzUrl",
        //   uplaod: (e) => this.handleUploadYYZZCard(e),
        //   rules: [{ required: true, message: "请上传", trigger: "blur" }],
        //   defaultValue: "",
        // },
        {
          valueType: "input",
          type: "textarea",
          label: "备注",
          dataIndex: "note",
          defaultValue: "",
        },
      ],
      isEdit: false,
    };
  },
  methods: {
    async handleUploadIdCard(e) {
      uploadImg(e, 1024 * 1024, {},'head', (res) => {
        searchFormParams(this, "idUrl").defaultValue = res;
      });
    },
    async handleUploadYYZZCard(e) {
      uploadImg(e, 1024 * 1024, {},'head', (res) => {
        searchFormParams(this, "yyzzUrl").defaultValue = res;
      });
    },
    open(row) {
    

      row ? (this.isEdit = true) : (this.isEdit = false);
      this.$refs.baseDialog.open();
      // 重新赋值 fromIems 以触发 watch 监听器
      this.fromIems = _.cloneDeep(this.fromIems);
      this.$nextTick(() => {
        if (row) {
          for (var k in row) {
            this.$set(this.$refs.BaseForm.form, k, row[k]);
            searchFormParams(this, "idUrl").defaultValue = row['idUrl'];
          }
        } else {
          this.$refs.BaseForm.form = {};
        }
      });
    },
    close() {
      this.$refs.BaseForm.form = {};
      searchFormParams(this, "idUrl").defaultValue = '';
      // searchFormParams(this, "yyzzUrl").defaultValue = '';
      this.$refs.baseDialog.close();
      this.$refs.BaseForm.rest();
    },
    confrim() {
      if (this.isEdit) {
        this.$refs.BaseForm.edit();
      } else {
        this.$refs.BaseForm.submit();
      }
    },
    async submit(data) {
      const res = await getId({ type: "org" });
      let params = { ...data };
      params.orgCode = res.content.id;
      params.isApply = 0; //平台 1入驻
      const result = await add(params);
      if (result) {
        Message.success("添加成功");
        this.$emit("success");
        this.close();
      }
    },
    async edit(data) {
      const res = await edit({
        orgCode: data.orgCode,
        name: data.name,
        xm: data.xm,
        phone: data.phone,
        idUrl: data.idUrl,
        yyzzUrl: data.yyzzUrl,
        note: data.note,
        isApply: data.isApply,
      });
      if (res) {
        this.$emit("success");
        Message.success("success");
        this.close();
      }
    },
  },
};
</script>

<style lang="scss" scoped></style>
